package com.irun.pm.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.AbsoluteLayout;

/**
 * 一个绝对布局
 * 
 * @author Administrator
 * 
 */
@SuppressWarnings("deprecation")
public class ViewScroll extends AbsoluteLayout {
	private int screenW; // 可用的屏幕宽
	private int screenH; // 可用的屏幕高 总高度-上面组件的总高度
	private static int imgW; // 图片原始宽
	private static int imgH; // 图片原始高
	private TouchView tv;
	private int degree = 0;
	private Matrix matrix = new Matrix();
	private Bitmap img;
	Bitmap resizeBitmap;
	int layout_w = 0;
	int layout_h = 0;
	Drawable image;
	private Bitmap[] bmpArray = null;
	private float beforeLenght; // 两触点距离
	private float afterLenght; // 两触点距离
	private float scale = 0.04f; // 缩放的比例 X Y方向都是这个值 越大缩放的越快,用于多点触摸下
	private int mode = NONE; // 当前的事件
	static final int NONE = 0;
	static final int DRAG = 1; // 拖动中
	static final int ZOOM = 2; // 缩放中
	static final int BIGGER = 3; // 放大ing
	static final int SMALLER = 4; // 缩小ing
	private static final String TAG = "ViewScroll";

	public ViewScroll(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ViewScroll(Context context) {
		super(context);
	}

	public void start(Context context, Bitmap bmp) {
		this.removeAllViews();
		bmpArray = new Bitmap[4];
		screenW = ((Activity) context).getWindowManager().getDefaultDisplay()
				.getWidth();
		screenH = ((Activity) context).getWindowManager().getDefaultDisplay()
				.getHeight();
		tv = new TouchView(context, screenW, screenH);
		img = bmp;
		init();
		Log.i(TAG, "img.getWidth() = "+img.getWidth());
		this.addView(tv);
	}

	public void zoomImg(boolean zoom) {
		if (zoom) {
			// 放大
			if (tv.getWidth() < imgW * 3)
				tv.setScale(0.2f, 3);
		} else {
			// 缩小
			if (imgW >= screenW && tv.getWidth() > screenW) {
				tv.setScale(0.2f, 4);
				if (tv.getWidth() < screenW) {
					if (imgW > imgH) {
						layout_w = imgW > screenW ? screenW : imgW; // 实际显示的宽
						layout_h = imgH > screenH ? (int) (imgH * ((float) screenW / (float) imgW))
								: imgH; // 实际显示的高
					} else {
						layout_w = imgW > screenW ? (int) (imgW * ((float) screenH / (float) imgH))
								: imgW; // 实际显示的宽
						layout_h = imgH > screenH ? screenH : imgH; // 实际显示的高
					}
					tv
							.setLayoutParams(new AbsoluteLayout.LayoutParams(
									layout_w, layout_h, layout_w == screenW ? 0
											: (screenW - layout_w) / 2,
									layout_h == screenH ? 0
											: (screenH - layout_h) / 2));
				}
			} else if (imgW < screenW && tv.getWidth() > imgW) {
				tv.setScale(0.2f, 4);
				if (tv.getWidth() < img.getWidth()) {
					int layout_w = imgW > screenW ? screenW : imgW; // 实际显示的宽
					int layout_h = imgH > screenH ? screenH : imgH; // 实际显示的高
					tv
							.setLayoutParams(new AbsoluteLayout.LayoutParams(
									layout_w, layout_h, layout_w == screenW ? 0
											: (screenW - layout_w) / 2,
									layout_h == screenH ? 0
											: (screenH - layout_h) / 2));
				}
			}
			tv.resetPosition();
		}

	}

	int direct = 1;

	public void rotate(boolean rotate) {
		if (!rotate) {
			degree += 90;
			matrix.setRotate(degree);

		} else {
			degree -= 90;
			matrix.setRotate(degree);
		}
		if (degree % 360 > 0) {
			switch (degree % 360) {
			case 0:
				direct = 1;
				break;
			case 90:
				direct = 2;
				break;
			case 180:
				direct = 3;
				break;
			case 270:
				direct = 4;
				break;

			default:
				break;
			}
		} else {
			switch (-(degree % 360)) {
			case 0:
				direct = 1;
				break;
			case 90:
				direct = 4;
				break;
			case 180:
				direct = 3;
				break;
			case 270:
				direct = 2;
				break;

			default:
				break;
			}
		}
		init();
	}

	private void init() {
		if (bmpArray[direct - 1] == null) {
			resizeBitmap = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img
					.getHeight(), matrix, true);
			bmpArray[direct - 1] = resizeBitmap;
		} else {
			resizeBitmap = bmpArray[direct - 1];
		}
		image = new BitmapDrawable(resizeBitmap);
		tv.setImageDrawable(image);
		imgW = resizeBitmap.getWidth();
		imgH = resizeBitmap.getHeight();
		if (imgW > imgH) {
			layout_w = imgW > screenW ? screenW : imgW; // 实际显示的宽
			layout_h = imgH > screenH ? (int) (imgH * ((float) screenW / (float) imgW))
					: imgH; // 实际显示的高
			if (layout_h > screenH) {
				layout_h = screenH;
				layout_w = (int) (layout_w * ((float) screenH / (float) layout_h));
			}
		} else {
			layout_w = imgW > screenW ? (int) (imgW * ((float) screenH / (float) imgH))
					: imgW; // 实际显示的宽
			layout_h = imgH > screenH ? screenH : imgH; // 实际显示的高
			if (layout_w > screenW) {
				layout_w = screenW;
				layout_h = (int) (layout_h * ((float) screenW / (float) layout_w));
			}
		}

		tv.setLayoutParams(new AbsoluteLayout.LayoutParams(layout_w, layout_h,
				layout_w == screenW ? 0 : (screenW - layout_w) / 2,
				layout_h == screenH ? 0 : (screenH - layout_h) / 2));

	}

	public static int getImgWidth() {
		return imgW;
	}

	public static int getImgHeight() {
		return imgH;
	}

	/**
	 * 处理触碰..
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			mode = DRAG;

			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			if (spacing(event) > 10f) {
				mode = ZOOM;
				beforeLenght = spacing(event);
			}
			break;
		case MotionEvent.ACTION_UP:
			mode = NONE;
			break;
		case MotionEvent.ACTION_POINTER_UP:
			mode = NONE;
			break;
		case MotionEvent.ACTION_MOVE:
			/* 处理缩放 */
			if (mode == ZOOM) {
				Log.i(TAG, "tv.getWidth() = "+tv.getWidth());
				if (spacing(event) > 10f) {
					afterLenght = spacing(event);
					float gapLenght = afterLenght - beforeLenght;
					if (gapLenght == 0) {
						break;
					} else if (Math.abs(gapLenght) > 5f) {
						if (gapLenght > 0) {
							// 放大
							if (tv.getWidth() < imgW * 3) {
								tv.setScale(scale, BIGGER);
							}
						} else {
							// 缩小
							if (imgW >= screenW && tv.getWidth() > screenW) {
								tv.setScale(scale, SMALLER);
							} else if (tv.getWidth() > imgW) {
								tv.setScale(scale, SMALLER);
							}

						}
						beforeLenght = afterLenght;
					}
				}
			}
			break;
		}
		return true;
	}

	/**
	 * 计算两点间的距离
	 */
	private float spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}

}
